<?php
/**
 * Author: yixuan
 * Desc:
 * Time: 2016/5/8 21:48
 */

namespace app\web\controller;

use app\model\Modules;
use app\model\Theme;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Base extends Controller
{
    private $isLogin;
    private $userInfo;

    public function __construct()
    {
        global $_G, $_P;
        $main_domain = !empty(DOMAIN_YIXUAN) ? DOMAIN_YIXUAN : DOMAIN0;
        if (!empty($main_domain) && $_SERVER['HTTP_HOST'] !== $main_domain && strstr($_SERVER['HTTP_HOST'], "yixuanrj.cn") == false) {
            $url = 'https://' . $main_domain . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];

            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='$url';";
            echo "</script>";
            exit;
        }

        if (!$this->checkUserLogin()) {
            if ($_P['do'] != 'login') {
                $action = \think\Request::instance()->action();
                $now_ac = strtolower($_P['do'] . '/' . $action);
                $login_url = $_G['url_domain'] . 'login.php';
                if ($now_ac == 'entry/index') {
                    $this->redirect($login_url);
                } else {
                    $login_url = $login_url . "?referer=" . urlencode($_G['url_full']);
                    if (request()->isAjax()) {
                        return_json(0, '请先登录');
                    } else {
                        return_msg('请先登录', $login_url, 'error', 2);
                    }
                }
            }
        } elseif ($_P['do'] == 'login' && $_P['op'] == 'home') {
            $user = Session::get('userinfo');
            if ($user['user_level'] > 2) {
                $module = $user['module_name'];
                $login_url = createWebUrl('theme', array('op' => 'display', 'cw' => 'home', 'm' => $module), true);
            } else {
                $login_url = createWebUrl('yxmodule', ['op' => 'display'], true);
            }
            $this->redirect($login_url);
        }
        $_G['mpname'] = '未配置';
        if (!empty($_G['mpid'])) {
            $platform_info = Modules::getPlatform($_G['mpid']);
            if (!empty($platform_info)) {
                $_G['mpname'] = $platform_info['name'];
            }
        }
    }

    /**
     * Desc:验证用户登录并返回用户信息
     * Author: yixuan
     * Time: 2016/5/8 22:04
     * @return bool
     */
    public function checkUserLogin()
    {
        global $_G, $_P;
        $tmpUserInfo = Session::has('userinfo');
        if (!$tmpUserInfo) {
            if ($_COOKIE['username'] && $_COOKIE['single']) {
                $end_time = strtotime(date('Y-m-d', time()) . ' 23:59:59');
                $post['username'] = $_COOKIE['username'];
                $post['single'] = $_COOKIE['single'];
                $url = 'https://oa.yixuan.net/web/single.php';
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                $result = curl_exec($ch);
                curl_close($ch);
                $res_array = json_decode($result, true);
                if ($res_array['code'] == 0) {
                    setcookie("autologin", 1, $end_time);
                    $_P['username'] = $res_array['data']['username'];
                    $_P['password'] = $res_array['data']['password'];
                    $_P['autologin'] = 1;
                    Login::post_login();
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        $user = Session::get('userinfo');
        $_G['userid'] = $user['userid'];
        $_G['userlevel'] = $user['user_level'];
        $_G['isSuperAdmin'] = ($_G['userlevel'] == 1 || $_G['userlevel'] == 2) ? ture : false;
        $_G['username'] = $user['username'];
        $_G['realname'] = $user['realname'];
        if ($_G['isSuperAdmin'] && $_COOKIE['autologin'] == 1 && !$_COOKIE['singlestatus']) {
            return false;
        }
        if ($_G['userlevel'] > 2 && $_P['do'] != 'login') {
            $my_module_name = $user['module_name'];
            $do = strtolower($_P['do']);
            $check_authority = false;
            if (in_array($do, ['platform', 'yxmodule']) || ($do == 'users' && ($_P['op'] != 'myaccount' && $_P['op'] != 'myaccount_save'))) {
                $check_authority = true;
            }
            if ($my_module_name != $_P['m'] || $check_authority) {
                $forward = createWebUrl('theme', array('op' => 'display', 'cw' => 'home', 'm' => $my_module_name));
                $this->redirect($forward);
            }
        }
        return true;
    }

    public function return_ajax_json($code = 1, $msg = '操作成功', $data = [])
    {
        exit(json_encode(['code' => $code, 'msg' => $msg, 'data' => $data]));
    }

}